package com.tsystems.train.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;

import com.tsystems.train.entity.TimeTableElement;
import com.tsystems.train.entity.User;
import com.tsystems.train.entity.UserRoles;
import com.tsystems.train.service.TimeTableElementServices;
import com.tsystems.train.service.UserServices;
import com.tsystems.train.validator.MyValidator;

@Named("userTableBean")
@RequestScoped
public class UserTableBean implements Serializable {

	private static final long serialVersionUID = 2165793273424547096L;

	@EJB
	private UserServices userService;

	@EJB
	private TimeTableElementServices tteService;

	private String name;
	private String surname;
	private String password;
	private String confirm;
	private String login;
	private Date birthday;
	private UserRoles role;

	private UserRoles[] roles = { UserRoles.PASSENGER, UserRoles.EMPLOYEE };
	private List<TimeTableElement> ttes;
	private TimeTableElement tteSelected;

	private UserTableDataModel userTableDataModel;
	private User selectedUser = new User();

	public UserTableBean() {
		userTableDataModel = new UserTableDataModel();
		selectedUser = new User();
	}

	@PostConstruct
	public void init() {
		TimeTableConverter.ttes = tteService.viewTimeTableElementsBase();
		ttes = TimeTableConverter.ttes;
		updateTable();
	}

	public void updateTable() {
		List<User> result = userService.viewPassengersBase();
		userTableDataModel.setWrappedData(result);

	}

	private boolean validate() {
		if (!password.equals(confirm)) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Password and confirm not match!", null));
			return false;
		}

		return true;
	}

	public String addUser() {
		if (!validate()) {
			return null;
		}

		login.trim();
		password.trim();
		name.trim();
		surname.trim();

		User user = new User(login, password, name, surname, birthday, role);

		if (!MyValidator.validate(user)) {
			return null;
		}

		String result = userService.register(user);
		if (result.equals("New user registered!")) {
			updateTable();
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							"New user registered!", null));
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "Add error",
							result));
		}
		return null;
	}

	public String filterPassengers() {
		if (tteSelected == null) {
			updateTable();
			return null;
		}
		List<User> result = userService.viewPassengersOnTrain(tteSelected);
		userTableDataModel.setWrappedData(result);
		return null;
	}

	public void deleteUser() {
		if (selectedUser != null) {
			String result = userService.removePassenger(selectedUser);
			if (result.equals("User successfully deleted")) {
				updateTable();

			}
		}
	}

	public User getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}

	public UserTableDataModel getUserTableDataModel() {
		return userTableDataModel;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public UserRoles getRole() {
		return role;
	}

	public void setRole(UserRoles role) {
		this.role = role;
	}

	public String getConfirm() {
		return confirm;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public UserRoles[] getRoles() {
		return roles;
	}

	public void setRoles(UserRoles[] roles) {
		this.roles = roles;
	}

	public void onEdit(RowEditEvent event) {
		User editedUser = (User) event.getObject();
		if (!MyValidator.validate(editedUser)) {
			return;
		}
		String result = userService.editPassenger(editedUser);
		if (result.equals("User successfully changed")) {
			FacesMessage msg = new FacesMessage("User Edited",
					((User) event.getObject()).getLogin());

			FacesContext.getCurrentInstance().addMessage(null, msg);
			updateTable();
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Delete error", result));
		}
	}

	public void onCancel(RowEditEvent event) {
		FacesMessage msg = new FacesMessage("User edit cancelled",
				((User) event.getObject()).getLogin());

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public List<TimeTableElement> getTtes() {
		return ttes;
	}

	public void setTtes(List<TimeTableElement> ttes) {
		this.ttes = ttes;
	}

	public TimeTableElement getTteSelected() {
		return tteSelected;
	}

	public void setTteSelected(TimeTableElement tteSelected) {
		this.tteSelected = tteSelected;
	}

}
